GameLayer = cc.Layer.extend({
	game: null,
	CurrentLevel: 1,
	Winning: false,
	ctor: function (scene) {
		this._super();
		this.Scene = scene;
	},
	onEnter: function () {
		this._super();
		window.Layer = this;
		cc.eventManager.removeAllListeners();
		WinSize = cc.director.getWinSize();
		delete WinSize;
		this.addChild(this.Background = createBackground(), 0);
		this.setLevel(CookieHelper.get("level", window.btoa("1")));
		cc.eventManager.addListener(cc.EventListener.create({
			event: cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches: true,
			onTouchBegan: function (touch) {
				this.startX = touch.getLocationX();
				this.startY = touch.getLocationY();
				return true;
			},
			onTouchEnded: function (touch) {
				let xd = touch.getLocationX() - this.startX;
				let yd = touch.getLocationY() - this.startY;
				let xa = Math.abs(xd), ya = Math.abs(yd);
				if (Math.max(xa, ya) < 10) return true;
				Layer.game.processDirection(xa > ya ? (xd < 0 ? 0 : 2) : (yd < 0 ? 3 : 1));
			}
		}), this.Background);
		cc.eventManager.addListener(cc.EventListener.create({
			event: cc.EventListener.KEYBOARD,
			onKeyPressed: function (keyCode, event) {
				if (keyCode >= 37 && keyCode <= 40)
					Layer.game.processDirection(keyCode - 37);
			}
		}), this.Background);
		let FullScreen = cc.Sprite.create(R.Button.FullScreen);
		FullScreen.setPosition(FullScreen.width / 2 * 3 + 20, C.GameHeight - FullScreen.height / 2 - 10);
		cc.eventManager.addListener(cc.EventListener.create({
			event: cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches: true,
			E: document.documentElement,
			onTouchBegan: function (touch) {
				if (Layer.Winning) return false;
				if (!cc.rectContainsPoint(FullScreen.getBoundingBox(), touch.getLocation())) return false;
				if (cc.screen.fullScreen()) {
					cc.screen.exitFullScreen();
				} else {
					cc.screen.requestFullScreen(this.E, function () {
						window.onresize(null);
					});
				}
				return true;
			}
		}), FullScreen);
		FullScreen.onResize = function () {
			this.setPosition(this.width / 2 * 3 + 20, C.GameHeight - this.height / 2 - 10);
		};
		this.addChild(this.FullScreen = FullScreen, 2);
		let Exit = cc.Sprite.create(R.Button.Exit);
		Exit.setPosition(Exit.width / 2 + 10, C.GameHeight - Exit.height / 2 - 10);
		cc.eventManager.addListener(cc.EventListener.create({
			event: cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches: true,
			E: document.documentElement,
			onTouchBegan: function (touch) {
				if (!cc.rectContainsPoint(Exit.getBoundingBox(), touch.getLocation())) return false;
				cc.director.runScene(cc.TransitionSlideInT.create(1, new MainScene(), false));
				return true;
			}
		}), Exit);
		Exit.onResize = function () {
			this.setPosition(this.width / 2 + 10, C.GameHeight - this.height / 2 - 10);
		};
		this.addChild(this.Exit = Exit, 2);
		window.Layer = this;
	},
	onWin: function () {
		Layer.Winning = true;
		cc.audioEngine.playEffect(R.Sounds.Success);
		Layer.Scene.addChild(new WinLayer(Layer.Scene));
	},
	setLevel: function (level) {
		CookieHelper.set("level", level);
		level = Number(window.atob(String(level)));
		if (this.game)
			this.game.destroy();
		this.CurrentLevel = level;
		this.game = LoadGame(this, Levels[level]);
		this.game.onWin = this.onWin;
		this.game.draw();
	},
	onResize: function () {
		this.game.onResize();
		this.FullScreen.onResize();
		this.Exit.onResize();
		this.Background.onResize();
	}
});
WinLayer = cc.Layer.extend({
	ctor: function (scene) {
		this._super();
		this.Scene = scene;
	},
	onEnter: function () {
		this._super();
		this.LastLayer = Layer;
		window.Layer = this;
		let Rect = new cc.DrawNode();
		Rect.drawRect(cc.p(0, 0), cc.p(C.GameWidth, C.GameHeight), new cc.Color(0, 0, 0, 77));
		this.addChild(this.Rect = Rect);
		let Win = cc.Sprite.create(R.Success);
		Win.setPosition(C.GameWidth / 2, C.GameHeight / 2 + Win.height / 2 + 10);
		this.addChild(this.Win = Win);
		let Next = cc.Sprite.create(R.NextLevel);
		Next.setPosition(C.GameWidth / 2, C.GameHeight / 2 - Next.height / 2);
		let nextLevel = function () {
			WinLayer.Scene.removeChild(WinLayer);
			window.Layer = Layer.LastLayer;
			cc.eventManager.removeListener(Listener);
			cc.eventManager.removeListener(Listener2);
			Layer.setLevel(window.btoa(String(Layer.CurrentLevel + 1)));
		};
		let WinLayer = this;
		let Listener2 = cc.EventListener.create({
			event: cc.EventListener.KEYBOARD,
			onKeyPressed: function (keyCode, event) {
				if (keyCode === cc.KEY.enter)
					nextLevel();
			}
		});
		let Listener = cc.EventListener.create({
			event: cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches: true,
			onTouchBegan: function (touch) {
				if (!cc.rectContainsPoint(Next.getBoundingBox(), touch.getLocation())) return false;
				nextLevel();
				return true;
			}
		});
		cc.eventManager.addListener(Listener, Next);
		cc.eventManager.addListener(Listener2, Next);
		this.addChild(this.Next = Next);
	},
	onResize: function () {
		let Win = this.Win;
		let Next = this.Next;
		this.Rect.drawRect(cc.p(0, 0), cc.p(C.GameWidth, C.GameHeight), new cc.Color(0, 0, 0, 77));
		Win.setPosition(C.GameWidth / 2, C.GameHeight / 2 + Win.height / 2 + 10);
		Next.setPosition(C.GameWidth / 2, C.GameHeight / 2 - Next.height / 2);
	}
});
GameScene = cc.Scene.extend({
	onEnter: function () {
		this._super();
		this.addChild(new GameLayer(this));
	}
});